import numpy as np
import os
import matplotlib
import matplotlib.pyplot as plt
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12
X = 6*np.random.rand(100,1)-3
print(X)
y = 2+4*X+10*X**2 +np.random.randn(100,1)
plt.plot(X,y,'r.')
plt.xlabel('x')
plt.ylabel('y')
plt.axis([-2,2,0,15])
plt.show()

X_process = np.hstack((np.ones((100,1)),X))
print(X_process)
w_best = np.linalg.inv((X_process.T.dot(X_process))).dot(X_process.T).dot(y)
print(w_best)

from sklearn.preprocessing import PolynomialFeatures
poly_features = PolynomialFeatures(degree=2,include_bias=False)
X_poly_process = poly_features.fit_transform(X)
from sklearn.linear_model import LinearRegression
linRg = LinearRegression()
linRg.fit(X_poly_process,y)
#权重
print(linRg.coef_)
#偏置项
print(linRg.intercept_)

